package com.handinglian.ai.notice.service.impl;

import com.handinglian.ai.common.exception.BizException;
import com.handinglian.ai.common.util.FastJsonUtil;
import com.handinglian.ai.common.util.GeneralUtil;
import com.handinglian.ai.notice.dto.KeyGroupDto;
import com.handinglian.ai.notice.entity.KeyGroup;
import com.handinglian.ai.notice.entity.KeyGroupDetail;
import com.handinglian.ai.notice.mapper.KeyGroupMapper;
import com.handinglian.ai.notice.param.KeyGroupCreateParam;
import com.handinglian.ai.notice.param.KeyGroupUpdateParam;
import com.handinglian.ai.notice.service.KeyGroupDetailService;
import com.handinglian.ai.notice.service.KeyGroupService;
import com.handinglian.ai.notice.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class KeyGroupServiceImpl implements KeyGroupService {
    @Autowired
    private KeyGroupMapper keyGroupMapper;
    @Autowired
    private KeyGroupDetailService keyGroupDetailService;
    @Autowired
    @Lazy
    private NoticeService noticeService;

    @Override
    @Transactional
    public int create(KeyGroupCreateParam createParam) {
        KeyGroup keyGroup = new KeyGroup();
        keyGroup.setCreateTime(new Date());
        keyGroup.setName(createParam.getName());
        int amount = keyGroupMapper.insertSelective(keyGroup);

        List<KeyGroupDetail> keyGroupDetails = FastJsonUtil.ListToList(createParam.getDetailCreateParams(), KeyGroupDetail.class);
        keyGroupDetails.forEach(keyGroupDetail -> {
            keyGroupDetail.setKeyGroupId(keyGroup.getId());
        });
        keyGroupDetailService.createBatch(keyGroupDetails);

        return amount;
    }

    @Override
    public List<KeyGroupDto> list() {
        return keyGroupMapper.find();
    }

    @Override
    public KeyGroupDto get(Integer id) {
        return keyGroupMapper.findById(id);
    }

    @Override
    @Transactional
    public int delete(Integer id) {
        Integer count = noticeService.countKeyGroup(id);
        if (count > 0){
            throw BizException.KEY_GROUP_USED_ERROR;
        }
        keyGroupDetailService.delete(id);
        return keyGroupMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int update(KeyGroupUpdateParam updateParam) {
        keyGroupDetailService.delete(updateParam.getId());
        List<KeyGroupDetail> details = FastJsonUtil.ListToList(updateParam.getDetailUpdateParams(), KeyGroupDetail.class);
        details.forEach(detail ->{
            detail.setKeyGroupId(updateParam.getId());
        });
        keyGroupDetailService.createBatch(details);

        KeyGroup keyGroup = FastJsonUtil.ObjectToObject(updateParam, KeyGroup.class);
        return keyGroupMapper.updateByPrimaryKeySelective(keyGroup);
    }
}
